package com.ww.demo.core.service.impl;

import com.ww.demo.core.dao.UserMapper;
import com.ww.demo.core.entity.UserInfo;
import com.ww.demo.core.entity.response.LoginResult;
import com.ww.demo.core.exception.ClientException;
import com.ww.demo.core.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author liuqingwang
 * @date 2021/10/25 21:55
 */
@Service
public class UserServiceImpl implements UserService {

    public static final Logger LOGGER = LoggerFactory.getLogger(UserServiceImpl.class);

    @Autowired
    UserMapper userMapper;

    @Override
    public LoginResult login(String account, String password) {
        LOGGER.info("account : {}, password : {}", account, password);
        UserInfo userInfo = userMapper.selectByAccount(account);
        if (userInfo == null) {
            throw new ClientException("登录失败，用户名或密码错误！");
        }
        String token = "md5-token-" + (System.currentTimeMillis() / 1000);
        return LoginResult.create(token, userInfo);
    }

    @Override
    public List<UserInfo> getUserList(Integer start, Integer rows) {
        List<UserInfo> userInfoList = userMapper.getUserList(start, rows);
        return userInfoList;
    }
}
